package com.lamp.tabgroups;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.lamp.main.HomescreenActivity;
import com.lamp.main.R;

public class FeedGroup extends ActivityGroup {

	public static FeedGroup group;
	public static int popUpPosition;
	public static int clickPosition = 10;
	private ArrayList<View> history;
	private View[] mainViews = new View[9];
	private LinearLayout nField;
	// private ArrayList<View> historyTop;
	Animation outToLeft;
	Animation inFromRight;
	public float y;
	public float x;
	Button mainBack;
	Button friend;
	Button sms;
	Button mainModul;
	LinearLayout mainCenter;
	public boolean clickM = false;
	public boolean clickF = false;
	public boolean realClick = false;

	public FeedGroup() {

	}

	public void popUpPosition() {
		this.popUpPosition = history.size();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.history = new ArrayList<View>();
		// this.historyTop = new ArrayList<View>();
		group = this;
		nField = (LinearLayout) getParent().findViewById(R.id.navigation);
		mainBack = (Button) getParent().findViewById(R.id.mainBack_btn);
		mainModul = (Button) getParent().findViewById(R.id.homeModul);
		mainCenter = (LinearLayout) getParent().findViewById(R.id.homeCenterlt);
		friend = (Button) getParent().findViewById(R.id.homeFriBtn);
		sms = (Button) getParent().findViewById(R.id.homeSmsBtn);

		View view = getLocalActivityManager().startActivity(
				"ListView",
				new Intent(this, HomescreenActivity.class)
						.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
				.getDecorView();
		replaceView(view);
	}

	public void goTo(Intent intent) {
		View view = getLocalActivityManager().startActivity("ListView",
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
		if (clickF || clickM) {
			realClick = false;
			popUpPosition();
			friend.setClickable(false);
			sms.setClickable(false);
			nField.setAnimation(AnimationUtils.loadAnimation(this,
					R.anim.out_to_left));
			nField.setVisibility(View.GONE);
		}
		replaceView(view);
	}

	public void goTo(Intent intent, boolean k, int position) {
		View view = null;
		if (position != 10) {
			if (mainViews[position] == null) {
				view = getLocalActivityManager().startActivity("Acivity",
						intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView();

				mainViews[position] = view;
			} else {
				view = mainViews[position];
				// if (position == 1)
				// RoundMapActivity.map.onResume();
			}
		} else {
			view = getLocalActivityManager().startActivity("Acivity",
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
		}
		if (k) {
			Animation inFromTop = AnimationUtils.loadAnimation(this,
					R.anim.in_from_top);
			Animation outToBottom = AnimationUtils.loadAnimation(this,
					R.anim.grow_from_bottom);
			replaceView(view, inFromTop, outToBottom);
		} else {
			Log.v("position", position + "");
			replaceView(view, position);
		}
	}

	public void replaceView(View v, Animation inAnim, Animation outAnim) {
		if (history.size() != 0) {
			View v1 = history.get(history.size() - 1);
			v1.setAnimation(outAnim);
			v.setAnimation(inAnim);
		}
		// if (clicked)
		// historyTop.add(v);
		// else
		history.add(v);
		setContentView(v);
	}

	public void replaceView(View v, Animation inAnim) {
		if (history.size() != 0) {
			v.setAnimation(inAnim);
		}
		history.add(v);
		setContentView(v);
	}

	public void replaceView(View v, int position) {
		ScaleAnimation scaleAnim = new ScaleAnimation((float) 0.1, (float) 1.0,
				(float) 0.1, (float) 1.0, x, y);
		AlphaAnimation alphaAnim = new AlphaAnimation((float) 0.3, (float) 1.0);
		scaleAnim.setDuration(250);
		alphaAnim.setDuration(250);
		AnimationSet setAnim = new AnimationSet(true);
		setAnim.addAnimation(scaleAnim);
		setAnim.addAnimation(alphaAnim);

		v.setAnimation(setAnim);
		AlphaAnimation beforeAnim = new AlphaAnimation((float) 0.8, (float) 0.0);
		beforeAnim.setDuration(250);
		history.get(history.size() - 1).setAnimation(beforeAnim);
		history.add(v);
		setContentView(v);
	}

	public void replaceView(View v) {
		if (history.size() != 0) {

			mainBack.setAnimation(AnimationUtils.loadAnimation(this,
					R.anim.shrink_from_top));
			mainModul.setAnimation(AnimationUtils.loadAnimation(this,
					R.anim.shrink_from_top));
			mainCenter.setAnimation(AnimationUtils.loadAnimation(this,
					R.anim.shrink_from_top));
			outToLeft = AnimationUtils.loadAnimation(this, R.anim.out_to_left);
			View v1 = history.get(history.size() - 1);
			v1.setAnimation(outToLeft);
			inFromRight = AnimationUtils.loadAnimation(this,
					R.anim.in_from_right);
			v.setAnimation(inFromRight);
		}
		// if (clicked)
		// historyTop.add(v);
		// else
		history.add(v);
		setContentView(v);
	}

	public void back() {
		Animation inFromLeft = AnimationUtils.loadAnimation(this,
				R.anim.in_from_left);
		Animation outToRight = AnimationUtils.loadAnimation(this,
				R.anim.out_to_right);
		Animation header = AnimationUtils.loadAnimation(this,
				R.anim.shrink_from_bottom);
		if (history.size() <= 3) {
			if (history.size() == 3 && (clickF || clickM)) {

			} else {

			}
		}
		Log.v("history size", "" + history.size());

		mainModul.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

			}
		});

		// if (clicked)
		// if (historyTop.size() > 1) {
		// View v = historyTop.get(historyTop.size() - 1);
		// v.setAnimation(outToRight);
		// historyTop.remove(historyTop.size() - 1);
		// v = historyTop.get(historyTop.size() - 1);
		// v.setAnimation(inFromLeft);
		// setContentView(v);
		// } else {
		// back_up();
		// }
		// else {

		Log.v("FeedGroup", FeedGroup.group.realClick + "");

		if (!FeedGroup.group.realClick) {
			if (history.size() > 1) {
				header.setFillBefore(true);
				mainModul.setAnimation(header);
				mainCenter.setAnimation(header);
				mainBack.setAnimation(header);
				View v = history.get(history.size() - 1);
				v.setAnimation(outToRight);
				history.remove(history.size() - 1);
				v = history.get(history.size() - 1);
				v.setAnimation(inFromLeft);
				// if (v == mainViews[1])
				// RoundMapActivity.map.onResume();
				setContentView(v);
			} else {
				finish();
				// ActivityManager activityManager = (ActivityManager)
				// getSystemService(ACTIVITY_SERVICE);
				// activityManager.killBackgroundProcesses(getPackageName());
			}
			if (clickF || clickM) {
				if (history.size() == popUpPosition) {
					realClick = true;
					friend.setClickable(true);
					sms.setClickable(true);
					nField.clearAnimation();
					nField.setAnimation(header);
					nField.setVisibility(View.VISIBLE);
				}
			}
		} else {
			clickF = false;
			clickM = false;
			nField.clearAnimation();
			nField.setAnimation(AnimationUtils.loadAnimation(FeedGroup.this,
					R.anim.pop_up_sms_out));
			nField.setVisibility(View.GONE);
			friend.setClickable(true);
			sms.setClickable(true);
			realClick = false;
		}

		// }
	}

	// public void back_up() {
	// Animation inFromLeft = AnimationUtils.loadAnimation(this,
	// R.anim.grow_from_top);
	// Animation outToRight = AnimationUtils.loadAnimation(this,
	// R.anim.out_to_top);
	// View v;
	// Log.d("size", history.size() + " clicked " + clickM);
	// mainModul.setBackgroundResource(R.drawable.minii);
	// mainModul.setOnClickListener(new View.OnClickListener() {
	//
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	//
	// }
	// });
	// // if (clicked) {
	// // v = historyTop.get(historyTop.size() - 1);
	// // v.setAnimation(outToRight);
	// // historyTop.clear();
	// // } else {
	// v = history.get(history.size() - 1);
	// v.setAnimation(outToRight);
	// history.remove(history.size() - 1);
	// // }
	// v = history.get(history.size() - 1);
	// v.setAnimation(inFromLeft);
	// clickM = false;
	// setContentView(v);
	//
	// }

	// public void back_up(boolean t) {
	// Animation inFromLeft = AnimationUtils.loadAnimation(this,
	// R.anim.grow_from_top);
	// Animation outToRight = AnimationUtils.loadAnimation(this,
	// R.anim.out_to_top);
	// View v;
	// mainModul.setBackgroundResource(R.drawable.minii);
	// mainModul.setOnClickListener(new View.OnClickListener() {
	//
	// public void onClick(View v) {
	// // TODO Auto-generated method stub
	//
	// }
	// });
	// // if (clicked) {
	// // v = historyTop.get(historyTop.size() - 1);
	// // v.setAnimation(outToRight);
	// // historyTop.clear();
	// // } else {
	// v = history.get(history.size() - 1);
	// v.setAnimation(outToRight);
	// history.remove(history.size() - 1);
	// // }
	// v = history.get(0);
	// v.setAnimation(inFromLeft);
	// clickM = false;
	// setContentView(v);
	//
	// }

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	public void clear() {
		if (history.size() > 1) {

			// mainBack.setBackgroundResource(R.drawable.lamplogo3);
			// mainBack.setText("");
			// mainBack.setOnClickListener(new View.OnClickListener() {
			//
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			//
			// }
			// });
			// mainModul.setBackgroundResource(R.drawable.minii);
			mainModul.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {

				}
			});
			if ((clickF || clickM)) {
				friend.setClickable(true);
				sms.setClickable(true);
				if (popUpPosition == history.size()) {
					clickF = false;
					clickM = false;
					nField.setAnimation(AnimationUtils.loadAnimation(
							FeedGroup.this, R.anim.pop_up_sms_out));
				} else {
					View view = getLocalActivityManager().startActivity(
							"ListView",
							new Intent(this, HomescreenActivity.class)
									.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
							.getDecorView();
					View lastView = history.get(history.size() - 1);
					lastView.setAnimation(AnimationUtils.loadAnimation(
							FeedGroup.this, R.anim.pop_up_sms_out));
					view.setAnimation(AnimationUtils.loadAnimation(
							FeedGroup.this, R.anim.pop_up_sms_in));
					history.clear();
					history.add(view);
					setContentView(view);
				}
				nField.setVisibility(View.GONE);
				clickF = false;
				clickM = false;
			} else {
				ScaleAnimation test = new ScaleAnimation((float) 0.7,
						(float) 0.0, (float) 0.7, (float) 0, x, y);
				AlphaAnimation test1 = new AlphaAnimation((float) 1.0,
						(float) 0.0);
				test.setDuration(250);
				test1.setDuration(250);
				AnimationSet lol = new AnimationSet(true);
				lol.addAnimation(test1);
				lol.addAnimation(test);
				View view = getLocalActivityManager().startActivity(
						"ListView",
						new Intent(this, HomescreenActivity.class)
								.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
						.getDecorView();
				AlphaAnimation mainAlpha = new AlphaAnimation((float) 0.2,
						(float) 1.0);
				mainAlpha.setDuration(250);

				View lastView = history.get(history.size() - 1);
				lastView.setAnimation(lol);
				history.clear();
				// historyTop.clear();
				history.add(view);
				view.setAnimation(mainAlpha);
				setContentView(view);
			}
		} else {
			if ((clickF || clickM)) {
				friend.setClickable(true);
				sms.setClickable(true);
				clickF = false;
				clickM = false;
				nField.setAnimation(AnimationUtils.loadAnimation(
						FeedGroup.this, R.anim.pop_up_sms_out));
				nField.setVisibility(View.GONE);

			}
		}
	}

	@Override
	public void onBackPressed() {
		FeedGroup.group.back();
		return;
	}
}